import cn.edu.zut.cs.Model.Patient;
import cn.edu.zut.cs.Service.WPService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/HelloServlet")
public class HelloServlet extends HttpServlet {

    WPService wpService;
    Patient patient;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String pno=request.getParameter("pno");
        String name=request.getParameter("name");
        String sex=request.getParameter("sex");
        int age=Integer.parseInt(request.getParameter("age"));
        String status=request.getParameter("status");
        String wardId=request.getParameter("wardId");
        int bedId=Integer.parseInt(request.getParameter("bedId"));
        String familyPhone=request.getParameter("familyPhone");

        patient.setWardId(wardId);
        patient.setPno(pno);
        patient.setFamilyPhone(familyPhone);
        patient.setStatus(status);
        patient.setBedId(bedId);
        patient.setAge(age);
        patient.setSex(sex);
        patient.setName(name);

        wpService.insertPatient(patient);

        PrintWriter out = response.getWriter();

        out.println(pno);
        out.println(name);
        out.println(sex);
        out.println(age);
        out.println(status);
        out.println(wardId);
        out.println(bedId);
        out.println(familyPhone);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();

        out.println("From Servlet!");

    }

    @Autowired
    public void setPatientService(WPService wpService) {
        this.wpService =wpService;
    }

    @Autowired
    public void setPatient(Patient patient) {
        this.patient = patient;
    }
}
